package com.garen.fire.dao.impl;

import com.garen.fire.dao.IFireCaseInfoDao;
import com.garen.fire.entity.FireCaseInfo;
import com.garen.utils.DateUtils;
import com.garen.utils.MapBuilder;
import com.garen.common.BaseDao;
import com.garen.common.PageBean;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;


@Repository
public class FireCaseInfoDaoImpl extends BaseDao<FireCaseInfo> implements IFireCaseInfoDao {

	@PostConstruct
	private void init(){
		initOrm(FireCaseInfo.class);
	}
	
	//获取最新案情,升序排列，
	@Override
	public List<FireCaseInfo> getFireCaseInfoList(FireCaseInfo fireCaseInfo,List<Integer> orgIds){
		String sql = "SELECT  t1.*,t2.org_short_name as zgzd_name,t2.org_id "
				+ " FROM fire_case_info t1,fire_org t2 "
				+ " where t1.zgzd = t2.org_uuid and t1.bjsj > :date "
				+ " and t2.org_id in (:orgIds) and t1.status <> '8' "
				+ " ORDER BY t1.bjsj desc " ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("date", fireCaseInfo.getBjsj())
				 .put("orgIds", orgIds)
				 .build();
		return getListBySQL(sql,paramMap);
	}
	//获取最新案情,升序排列，
		@Override
		public List<FireCaseInfo> getFireCaseInfoListPad(FireCaseInfo fireCaseInfo,List<Integer> orgIds){
			String sql = "SELECT  t1.*,t2.org_short_name as zgzd_name,t2.org_id ,(select count(1) from fire_case_record where fire_ajbh=t1.ajbh) as usercount ,(SELECT group_concat(t3.code_name,'一辆')as cartype from fire_car c1,fire_case_record t2,fire_code t3 where c1.car_id=t2.fire_car and t3.code_value=c1.car_type and t3.code_type=3 and t2.fire_ajbh=t1.ajbh ) as cartype "
					+ " FROM fire_case_info t1,fire_org t2 "
					+ " where t1.zgzd = t2.org_uuid and t1.bjsj > :date "
					+ " and t2.org_id in (:orgIds) and t1.status <> '8' "
					+ " ORDER BY t1.bjsj desc " ;
			Map<String,Object> paramMap = new MapBuilder()
					 .put("date", fireCaseInfo.getBjsj())
					 .put("orgIds", orgIds)
					 .build();
			return getListBySQL(sql,paramMap);
		}	
	//获取时间段案情
	@Override
	public List<FireCaseInfo> getFireCaseInfoList(PageBean<FireCaseInfo> pb,List<Integer> orgIdList,Date kssj,Date jssj){
		String sql = "SELECT  t1.*,t2.org_short_name as zgzd_name,t2.org_id from fire_case_info t1,fire_org t2"
				+ " where t1.zgzd = t2.org_uuid and t2.org_id in (:orgIdList) and t1.bjsj between :kssj and :jssj " ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("orgIdList",orgIdList)
				 .put("kssj",kssj)
				 .put("jssj",jssj)
				 .build();
		return getPageListBySQL(pb,sql,paramMap,"t1.bjsj asc");
	}

	@Override
	public List<FireCaseInfo> getCaseInfoByCar(String carId) {
		String sql = "select * from fire_case_info where ajbh=(select fire_ajbh from fire_case_record where fire_car=:carId AND to_days(kssj) = to_days(now()) ORDER BY fire_date DESC LIMIT 1)" ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("carId",carId)
				 .build();
		List<FireCaseInfo> list=getListBySQL(sql,paramMap);
		return list;
	}


	@Override
	public  List<Map<String, Object>> getstatuscount(FireCaseInfo fireCaseInfo,Integer orgIds) {
		String sql = "SELECT   count(1) as casecount, SUM(t1.status=0) as case0,SUM(t1.status>0 and t1.status<8)as caseing,SUM(t1.status=8) as caseover" 
				+ " FROM fire_case_info t1,fire_org t2 " 
			+ " where t1.zgzd = t2.org_uuid and t1.bjsj > :date"
			+ " and t2.org_id =:orgIds";
		Map<String,Object> paramMap = new MapBuilder()
				 .put("date", fireCaseInfo.getBjsj())
				 .put("orgIds", orgIds)
				 .build();
		List<Map<String,Object>> list=queryForListMap(sql,paramMap);
		return list;
	}
	
	@Override
	public List<FireCaseInfo> getFireCaseInfoList(PageBean<FireCaseInfo> pb, FireCaseInfo fireCaseInfo) {
		String sql = "select t1.*,"
				+ " (select tt.org_short_name from fire_org tt where tt.org_uuid = t1.zgzd) as zgzd_name "
				+ " from fire_case_info t1 where 1 = 1 " ;
		if(StringUtils.isNotEmpty(fireCaseInfo.getZgzd())){
			sql += " and t1.zgzd = :orgUUID ";
		}
		Date kssj = fireCaseInfo.getBjsj(),jssj = null;
		if(kssj != null) {
			kssj = DateUtils.clearDate(kssj);
			Calendar cale = Calendar.getInstance();
			cale.setTime(kssj);
			cale.add(Calendar.DAY_OF_YEAR, 1);
			jssj = cale.getTime();
			sql += " and t1.bjsj between :kssj and :jssj ";
		}
		Map<String,Object> paramMap = new MapBuilder()
				.put("orgUUID", fireCaseInfo.getZgzd())
				.put("kssj", kssj)
				.put("jssj", jssj)
				 .build();
		return getPageListBySQL(pb,sql,paramMap,"ajbh desc");
	}

	@Override
	public FireCaseInfo getFireCaseInfoByAjbh(String ajbh) {
		// TODO Auto-generated method stub
		FireCaseInfo caseInfo = null;
		String sql = "select * from fire_case_info where ajbh = :ajbh";
		Map<String,Object> paramMap = new MapBuilder()
				 .put("ajbh", ajbh)
				 .build();
		List<FireCaseInfo> resultList = getListBySQL(sql, paramMap);
		if(resultList!=null&&resultList.size()>0)
		{
			caseInfo = resultList.get(0);
		}
		return caseInfo;
	}
	
	
	
		
		

}
